package com.adcc.loadsheet.monitor.util;

import com.adcc.loadsheet.monitor.entity.MonitorStatus;
import com.google.common.base.Strings;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 告警和重启枚举
 */
public enum AlarmEnum {

    // 去掉commod
    // =======================================模块名称枚举===============================================

    MODULE_BGS("BGS", "BGS服务", "",  "BGS"),

    MODULE_DATABASE("DATABASE", "数据库", "", "database"),

    MODULE_MQ("MQ", "本地MQ", "",  "MQ"),

    MODULE_APP_UPLOAD("APP_UPLOAD", "舱单拆解服务","",  "AppUpload"),

    MODULE_AIR_INFO("AIR_INFO", "舱单数据服务", "",  "airInfo"),

    MODULE_MSG_UP_CENTER("MSG_UP_CENTER", "转报中心服务", "",  "MsgUpCenter"),

    MODULE_WEBSITE("WEBSITE", "地服网站", "",  "webside"),

    MODULE_FORWARDER("FORWARDER", "舱单抓取程序", "",  "forwarder"),

    MODULE_MYSQL("MYSQL", "MySql服务", "", ""),

    // =======================================节点信息枚举===============================================
    // ---------------------------------------APPUPLOAD-------------------------------------
    // 舱单拆解服务-舱单拆解状态
    APPUPLOAD_LOADSHEET("appupload_loadsheet" ,"舱单拆解状态" , "LOADSHEET SPLIT", "" ),
    // 舱单拆解服务-舱单拆解状态
    APPUPLOAD_NTP("appupload_ntp" ,"NTP状态", "NTP",   "" ),

    // ---------------------------------------MSGUPCENTER-------------------------------------
    // 转报中心服务-ACARS接收状态
    MSGUPCENTER_DOWNLINK("msgupcenter_downlink" ,"ACARS接收" , "ACARS RECV", "" ),
    // 转报中心服务-ACARS发送状态
    MSGUPCENTER_UPLINK("msgupcenter_uplink" ,"ACARS发送", "ACARS SEND",  "" ),
    // 转报中心服务- NTP状态
    MSGUPCENTER_NTP("msgupcenter_ntp" ,"NTP状态", "NTP",  "" ),

    // ---------------------------------------BGS-------------------------------------
    // BGS服务-MQ状态
    BGS_MQ("bgs_mq" ,"BGS-MQ" , "BGS-MQ", "BGS-MQ" ),
    // BGS服务-AGS状态
    BGS_AGS("bgs_ags" ,"AGS",  "AGS",  "" ),
    // BGS服务-上行报文接收状态
    BGS_UPLINK("bgs_uplink" ,"BGS-Uplink",  "BGS-UPINK",  "" ),
    // BGS服务-下行报文接收状态
    BGS_DOWNLINK("bgs_downlink" ,"BGS-Downlink", "BGS-DOWNLINK",  "" ),

    // ---------------------------------------舱单抓取程序-------------------------------------
    // 舱单抓取程序-本地接口1状态
    FORWARDER_FIRSTPORGRAMUP("forwarder_firstporgramup" ,"本地接口1" , "", "forwarder1"),
    // 舱单抓取程序-航旅接口1状态
    FORWARDER_FIRSTUMEINTERFACEUP("forwarder_firstumeinterfaceup" ,"航旅接口1",  "", "" ),
    // 舱单抓取程序-本地接口2状态
    FORWARDER_SECONDPORGRAMUP("forwarder_secondporgramup" ,"本地接口2",  "", "forwarder2" ),
    // 舱单抓取程序-航旅接口2状态
    FORWARDER_SECONDUMEINTERFACEUP("forwarder_secondumeinterfaceup" ,"航旅接口2",  "",  "" ),

    // ---------------------------------------数据库-------------------------------------
    // 数据库-主数据库状态
    DATABASE_MASTER("database_master" ,"主数据库",  "MASTER DATABASE",  "" ),
    // 数据库-备数据库状态
    DATABASE_SLAVE("database_slave" ,"备数据库",  "SLAVE DATABASE",  "" ),

    // ---------------------------------------舱单数据接口-------------------------------------
    // 舱单数据接口-MQ状态
    AIRINFO_MQ("airinfo_mq" ,"MQ","MQ",   "" ),
    // 舱单数据接口-舱单发送状态
    AIRINFO_MSG("airinfo_msg" ,"舱单发送","LOADSHEET SEND",   "" ),

    // ---------------------------------------地服网站-------------------------------------
    //地服网站1
    WEBSITE1("website_web1" , "地服网站1" ,"URL1",  "webside1"),
    //地服网站2
    WEBSITE2("website_web2" , "地服网站2" ,"URL2", "webside2"),

    // ---------------------------------------地服MQ-------------------------------------
    // 地服MQ状态
    MQ_MQ("mq_mq" , "" ,"MQ",  ""),

    // ----------------------------------------mysql------------------------------------------
    // 地服mysql状态
    MONITOR_MYSQL("monitor_mysql" , "MySql服务" ,"", ""),

    // ----------------------------------------------------------------------------------
    ;

    //==============================================告警声音和弹框======================================================

    /**
     * 告警声音
     * 获取模块异常信息：
     * 舱单拆解服务（一般/严重）告警
     * @param item 模块名称
     * @param status 服务告警状态
     */
    public static String getAlarmModelInfo(String item, MonitorStatus status) {
        // 模块异常模板：舱单拆解服务（一般/严重）告警
        // 判断模块，拼接告警声音信息的所属模块
        String alarmContent = getItemNameByItem(item);

        // 模块异常模板：舱单拆解服务（一般/严重）告警
        // 判断模块状态，拼接告警声音信息
        if(!Strings.isNullOrEmpty(alarmContent)){
            alarmContent += "-";
            switch (status) {
                case NORMAL:
                    alarmContent += "恢复正常";
                    break;
                case WARNING:
                    alarmContent += "一般";
                    break;
                case ABNORMAL:
                    alarmContent += "严重";
                    break;
                default:
                    break;
            }
            alarmContent += "告警！";
        }
        // 返回格式：XXX服务（一般/严重）告警!
        return alarmContent;
    }

    /**
     * 告警弹框
     * 获取告警的节点异常信息
     *
     * @param alarmContents 异常的节点信息
     * @return
     */
    public static String getAlarmShowContent(Set<String> alarmContents) {
        String result = "";
        for (String item : alarmContents) {
            String itemName = getItemNameByItem(item);
            // 获取告警节点信息
            if(!Strings.isNullOrEmpty(itemName)){
                result += itemName + "异常！";
            }
        }
        return result;
    }

    //==============================================告警声音和弹框======================================================

    // 静态Map，用于存储item到AlarmEnum实例的映射
    private static final Map<String, AlarmEnum> ITEM_MAP = new HashMap<>();
    static {
        // 在静态初始化块中填充映射
        for (AlarmEnum alarmEnum : AlarmEnum.values()) {
            ITEM_MAP.put(alarmEnum.getItem(), alarmEnum);
        }
    }

    /**
     * 根据item获取itemName的方法
     */
    public static String getItemNameByItem(String item) {
        AlarmEnum alarmEnum = ITEM_MAP.get(item);
        if (alarmEnum != null) {
            // 返回itemName
            return alarmEnum.getItemName();
        }
        // 找不到对应的item
        return "";
    }

    /**
     * 判断是否相等
     */
    public Boolean equels(String suffixUrl) {
        if (this.getEnglishCommand().equals(suffixUrl)){
            return true;
        }
        return false;
    }

    /**
     * 模块或节点唯一标识，和忽略项的id保持一致
     */
    private final String item;

    /**
     * 模块或节点名称告警信息
     */
    private final String itemName;

    /**
     * mysql数据库日志节点名字
     */
    private final String mysqlLogName;


    /**
     * 英文描述
     */
    private final String englishCommand;


    AlarmEnum(String item, String itemName, String mysqlLogName, String englishCommand) {
        this.item = item;
        this.itemName = itemName;
        this.mysqlLogName = mysqlLogName;
        this.englishCommand = englishCommand;
    }

    public String getItem() {
        return item;
    }

    public String getItemName() {
        return itemName;
    }

    public String getMysqlLogName() {
        return mysqlLogName;
    }

    public String getEnglishCommand() {
        return englishCommand;
    }

}
